package com.jt.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.jt.common.exception.ServiceException;
import com.jt.common.vo.PageObject;
import com.jt.sys.dao.SysConfigDao;
import com.jt.sys.entity.SysConfig;
import com.jt.sys.service.SysConfigService;

@Service
public class SysConfigServiceImpl implements SysConfigService {
	@Autowired
	private SysConfigDao sysConfigDao;

	@Override
	public PageObject<SysConfig> findPageObjects(String name, Integer pageCurrent) {
		// 1.对参数进行合法验证
		if (pageCurrent == null || pageCurrent < 1) {
			throw new IllegalArgumentException("输入页码值不正确，pageCurrent=" + pageCurrent);
		}
		// 2.基于条件查询总记录数

		int rowCount = sysConfigDao.getRowCount(name);
		// 3.对查询总记录数的结果进行校验
		if (rowCount == 0)
			throw new ServiceException("没有查询数据");
		// 4.依据条件查询当前页数据
		int pageSize = 2;
		int startIndex = (pageCurrent - 1) * pageSize;
		List<SysConfig> records = sysConfigDao.findPageObjects(name, startIndex, pageSize);
		// 5.封装数据
		PageObject<SysConfig> po = new PageObject<>();
		po.setRowCount(rowCount);
		po.setRecords(records);
		po.setPageCurrent(pageCurrent);
		po.setPageSize(pageSize);
		// 6.返回数据
		return po;
	}

	@Override
	@Transactional
	public int deleteObjects(Integer... ids) {
		// 1对参数进行合法验证
		if (ids == null || ids.length == 0)
			throw new IllegalArgumentException("请选择删除的元素");
		// 执行删除操作
		int rows = sysConfigDao.deleteObjectById(ids);//
		// 判定删除结果
		if (rows == 0)
			throw new ServiceException("记录可能已经不存在了");
		// 返回数据
		return rows;
	}

	@Override
	@Transactional
	public int saveObject(SysConfig entity) {
		// 参数合法性验证
		if (entity == null) {
			throw new IllegalArgumentException("保存对象不能为空");
		}
		if (StringUtils.isEmpty(entity.getName())) {
			throw new IllegalArgumentException("name不能为空");
		}
		if (StringUtils.isEmpty(entity.getValue())) {
			throw new IllegalArgumentException("value不能为空");
		}

		// 保存数据
		int rows = sysConfigDao.insertObject(entity);

		// 对结果进行验证
		if (rows != 1) {
			throw new ServiceException("写入失败");
		}
		// 返回结果

		return 0;
	}

	@Override
	@Transactional
	public int updateObject(SysConfig entity) {
		// 合法性验证
		if (entity == null)
			throw new IllegalArgumentException("对象不能为空");
		if (StringUtils.isEmpty(entity.getName()))
			throw new IllegalArgumentException("参数名不能为空");
		if (StringUtils.isEmpty(entity.getValue()))
			throw new IllegalArgumentException("参数值不能为空");
		
		int rows = sysConfigDao.updateObject(entity);
		if(rows==0) throw new ServiceException("此记录可能已不存在");
		return rows;
	}

}
